<?php
namespace app\push\controller;
use think\worker\Server;

class Worker extends Server{
    protected $db = "";

    protected $socket = 'websocket://0.0.0.0:2346';
    /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $data)
    {
        //var_dump($this->db);
        //解析客户端发送的数据，变成数组
        parse_str($data,$data_arr);
        if($data_arr["type"] == "logout"){
            unset($this->connects[$data_arr["curr_user_id"]]);
        }elseif($data_arr["type"] == "getUsers"){
            //将当前连接用户的id和socket连接对象绑定
            $this->connects[$data_arr["curr_user_id"]] = $connection;
            //获取之前接收没有阅读的消息
            //$old_msg = $this->db->select('from_uid , to_uid , msg , time')->from('think_msg')->where("status=0 and to_uid="+$data_arr["curr_user_id"])->query();
            $old_msg = $this->db->query('select from_uid , to_uid , msg , time from think_msg where status= 0 and to_uid='.$data_arr["curr_user_id"]);
            var_dump($old_msg);
            foreach ($old_msg as $m) {
                $this->connects[$data_arr['curr_user_id']]->send("message:" . $m['msg'] . ",id:" . $m['from_uid'] . " 对 我留言 ");
            }

            unset($old_msg);

            //将通知完成的消息，修改状态status为1
            $this->db->update('think_msg')->cols(array('status'=>1))->where('to_uid='.$data_arr["curr_user_id"])->query();

        }elseif($data_arr["type"] == "send_msg_to_one"){

            //客户端发送消息给指定id的用户
            $curr_user_id=$data_arr['curr_user_id'];
            $to_user_id=$data_arr['to_user_id'];
            $msg=$data_arr['msg'];

            //根据js传递过来的接收信息用户id找到该用户的连接对象,完成信息发送
            if(@isset($this->connects[$to_user_id])){

                //给对方发信息
                $this->connects[$to_user_id]->send($curr_user_id .'对我说:' . "message:".$msg);
                //给当前用户自己发送信息
                $this->connects[$curr_user_id]->send("我对 $to_user_id 说 :message:".$msg);
            }else{
                //将发的信息存储到数据库
                $this->db->insert('think_msg')->cols(array(
                    'from_uid'=>$curr_user_id,
                    'to_uid'=>$to_user_id,
                    'msg'=>$msg,
                    'time'=>time(),
                    'status'=>0))->query();
                $this->connects[$curr_user_id]->send("message: 对方不在线!给 $to_user_id 留言成功!");

            }

        }elseif($data_arr["type"] == "send_msg_to_all"){
            //客户端发送消息给指定id的用户
            $curr_user_id=$data_arr['curr_user_id'];
            $msg=$data_arr['msg'];
            foreach($this->connects as $c){
                $c->send($curr_user_id . '对所有人说:' . "message:" . $msg);
            }
        }
    }

    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection)
    {

    }

    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection)
    {

        //var_dump($user);
        unset($this->worker->connections[$connection->id]);
        $user_k = array_keys($this->worker->connections);
        //var_dump($user_k);
        foreach ($this->worker->connections as $c) {
            $c->send("message:[" . $connection->id . "] from ip " . $connection->getRemoteIp() . " disconnect !");
            $c->send("users:" . implode("-", $user_k));
        }
    }
    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg)
    {
        echo "error $code $msg\n";
    }
    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker)
    {
        // 将db实例存储在全局变量中(也可以存储在某类的静态成员中)
        $this->db = new \Workerman\MySQL\Connection('localhost', '3306', 'root', 'root', 'hb');
    }
}
